package com.chtw.oasystem.service.impl;

import cn.hutool.core.util.IdUtil;
import com.chtw.oasystem.bean.FullUserInfo;
import com.chtw.oasystem.bean.Position;
import com.chtw.oasystem.bean.PositionExample;
import com.chtw.oasystem.common.changelogs.ChangeLogsUtils;
import com.chtw.oasystem.mapper.PositionMapper;
import com.chtw.oasystem.service.PositionService;
import com.chtw.oasystem.utils.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author chenhao03
 * @create 2020-11-21 10:25
 */
@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private ChangeLogServiceImpl changeLogService;

    @Override
    public List<Position> list() {
        return positionMapper.selectByExample(new PositionExample());
    }

    @Override
    public boolean create(HttpSession session, Position position) {
        try {
            FullUserInfo user = Session.getCurrentUser(session);
            if (user == null) {
                throw new RuntimeException("用户未登录");
            } else {
                position.setPositionId(IdUtil.simpleUUID());
                int isCreated = positionMapper.insert(position);
                if (isCreated > 0) {
                    changeLogService.saveChangeLog(ChangeLogsUtils.generatePositionChangeLog(user.getUserName(), position.getPositionName(), "增加"));
                    return true;
                }
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public Position info(int id) {
        return positionMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean edit(HttpSession session, Position position) {
        try {
            FullUserInfo user = Session.getCurrentUser(session);
            if (user == null) {
                throw new RuntimeException("用户未登录");
            } else {
                int isEdit = positionMapper.updateByPrimaryKeySelective(position);
                if (isEdit > 0) {
                    changeLogService.saveChangeLog(ChangeLogsUtils.generatePositionChangeLog(user.getUserName(), position.getPositionName(), "编辑"));
                    return true;
                }
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public boolean delete(HttpSession session, int id) {
        try {
            FullUserInfo user = Session.getCurrentUser(session);
            if (user == null) {
                throw new RuntimeException("用户未登录");
            } else {
                Position position = this.info(id);
                int isDel = positionMapper.deleteByPrimaryKey(id);
                if (isDel > 0) {
                    changeLogService.saveChangeLog(ChangeLogsUtils.generatePositionChangeLog(user.getUserName(), position.getPositionName(), "删除"));
                    return true;
                }
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
